// Package mimetyp impliments mime type specific functions.
package mimetype

import (
	"mime"
	"net/url"
	"strings"

	"code.google.com/awwwe-browser/data"
)

var htmlMimeType = mime.TypeByExtension(".html")

func AWWWEURL(urlstring string) string {
	purl, err := url.Parse(urlstring)
	if err == nil {
		i := strings.Index(purl.Host, data.PeriodString)
		if i >= 0 {
			purl.Host = data.AWWWE + purl.Host[i:]
			return purl.String()
		}
	}
	return data.EmptyString
}

func IsURLMimeTypeHTML(url string) bool {
	mimeType := MimeTypeFromURL(url)
	return len(mimeType) == 0 || mimeType == htmlMimeType
}

func MimeTypeFromURL(urlstring string) string {
	purl, err := url.Parse(urlstring)
	if err != nil {
		return data.EmptyString
	}
	i := strings.LastIndex(purl.Path, data.PeriodString)
	if i > 0 {
		return mime.TypeByExtension(purl.Path[i:])
	} else {
		return data.EmptyString
	}
}
